package com.hzhr.iot.collect.protocol.processor;

import com.hzhr.iot.collect.Global;
import com.hzhr.iot.collect.constant.MessageFieldKeyEnum;
import com.hzhr.iot.collect.protocol.util.CachedCommandUtil;
import com.hzhr.iot.collect.protocol.util.NbUtil;
import com.hzhr.iot.core.util.HzhrDateUtil;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.sdk.model.alarmData.AlarmDataModel;
import com.hzhr.iot.device.sdk.model.alarmData.AlarmDataPostModel;
import com.hzhr.iot.device.sdk.model.collectDevice.CollectDeviceModel;
import com.hzhr.iot.device.sdk.model.iotDevice.IotDeviceModel;
import com.hzhr.iot.device.sdk.service.Device_AlarmDataService;
import com.hzhr.iot.device.sdk.service.Device_CollectDeviceService;
import com.hzhr.iot.device.sdk.service.Device_IotDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * NBIOT 物联网表异常事件上传 处理器
 * @author 48310
 *
 */
@Component
public class MessageProcessor16 implements MessageProcessor {
	@Autowired
	Device_CollectDeviceService device_collectDeviceService;
	@Autowired
	Device_IotDeviceService device_iotDeviceService;
	@Autowired
	Device_AlarmDataService device_alarmDataService;
	@Autowired
	Global global;
	@Autowired
	CachedCommandUtil cachedCommandUtil;

	@Override
	public List<Map<MessageFieldKeyEnum, Object>> proccess(int protocolId, String deviceId, Map<MessageFieldKeyEnum, Object> parsedMessageObj) throws Exception {
		String alarmDateStr = parsedMessageObj.get(MessageFieldKeyEnum.ALARM_DATE).toString() + parsedMessageObj.get(MessageFieldKeyEnum.ALARM_TIME).toString();
		Date alarmDate = HzhrDateUtil.strToDate(alarmDateStr, "yyyyMMddHHmmss");

		String alarmTypeCode = parsedMessageObj.get(MessageFieldKeyEnum.ALARM_TYPE).toString();

		//查询物联网设备
		IotDeviceModel iotDeviceModel = null;
		String iotDeviceCode = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.IOT_DEVICE_ID));
		if (!StringUtils.isEmpty(iotDeviceCode)) {
			Map<String, String> header = new HashMap<>();
			header.put("token", global.getFirstToken());
			Map<String, Object> query = new HashMap<>();
			query.put("_show", "one");
			query.put("_equal_iotPlatformDeviceId", deviceId);
			iotDeviceModel = device_iotDeviceService.get(header, query).getModel();
		}
		if (iotDeviceModel == null) throw new Exception("物联网设备不存在");

		Integer iotDeviceId = iotDeviceModel.getId();

		//查询采集设备
		Map<String, String> header = new HashMap<>();
		header.put("token", global.getFirstToken());
		Map<String, Object> query = new HashMap<>();
		query.put("_show", "one");
		query.put("_equal_iotDeviceId", iotDeviceModel.getId());
		CollectDeviceModel collectDeviceModel = device_collectDeviceService.get(header, query).getModel();

		Integer collectDeviceId = null;
		if (collectDeviceModel != null) {
			collectDeviceId = collectDeviceModel.getId();
		}

		query = new HashMap<>();
		query.put("_show", "one");
		query.put("_show_info", true);
		query.put("_equal_iotDeviceId", iotDeviceId);
		query.put("_equal_currentTm", alarmDate.getTime());
		AlarmDataModel alarmDataModel = device_alarmDataService.get(header, query).getModel();
		if (alarmDataModel == null) {
			AlarmDataPostModel alarmDataPostModel = new AlarmDataPostModel();
			alarmDataPostModel.setIotDeviceId(iotDeviceId);
			alarmDataPostModel.setCollectDeviceId(collectDeviceId);
			alarmDataPostModel.setCurrentTm(alarmDate.getTime());
			alarmDataPostModel.setAlarmType(NbUtil.convertAlarmCodes(alarmTypeCode));
			device_alarmDataService.post(header, new HashMap<>(), alarmDataPostModel);
		}

		cachedCommandUtil.deleteCachedCommand(deviceId,
				parsedMessageObj.get(MessageFieldKeyEnum.IOT_DEVICE_ID).toString(),
				parsedMessageObj.get(MessageFieldKeyEnum.MESSAGE_ID).toString());
		return null;
	}
}
